#include "button.h"
#include <stdlib.h>
#include "gd32f10x.h"                   // Device header

#define BUTTON_TASK_PERIOD  30
#define KEY_PORT            GPIOB
#define KEY_PIN             GPIO_PIN_1

// 全系统全局变量
ButtonObjectTypeDef button[1];
static uint32_t button_read_pin(ButtonObjectTypeDef *self); /* 读取按键IO状态 */


void button_init(void)
{
	for(int i = 0; i < 1; i++) {
		button_object_init(&button[i]);
		button[i].id = i + 1;
	    button[i].read_pin = button_read_pin;
		button[i].dp_th = 200; 
		button[i].lp_th = 1500;  
	}
}

static uint32_t button_read_pin(ButtonObjectTypeDef *self)
{
    switch(self->id) {
        case 1:
            return ((uint32_t)gpio_input_bit_get(KEY_PORT, KEY_PIN)^ 1) ;
        default:
            return 0;
    }
}

/**
 * @brief 定时器回调进行按键扫描
 *
 * @param argument
 */
void button_timer_callback(void *argument)
{
    for(int i = 0; i < 1; i++)
    {
        button_task_handler(&button[i], BUTTON_TASK_PERIOD);
    }
}
